package com.ly.heart.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseMeteringUnitMapper;
import com.ly.heart.service.IKingBaseMeteringUnitService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseMeteringUnitEntity;

import javax.annotation.Resource;

/**
 * 金蝶-基础-计量单位Service业务层处理
 *
 * @author cxl
 * @date 2024-07-01
 */
@Service
public class KingBaseMeteringUnitServiceImpl extends ServiceImpl<KingBaseMeteringUnitMapper, KingBaseMeteringUnitEntity> implements IKingBaseMeteringUnitService {

    @Resource
    private KingBaseMeteringUnitMapper kingBaseMeteringUnitMapper;

    /**
     * 查询金蝶-基础-计量单位
     *
     * @param id 金蝶-基础-计量单位ID
     * @return 金蝶-基础-计量单位
     */
    @Override
    public KingBaseMeteringUnitEntity selectKingBaseMeteringUnitEntityById ( String id ) {
        return kingBaseMeteringUnitMapper.selectKingBaseMeteringUnitEntityById ( id );
    }

    /**
     * 查询金蝶-基础-计量单位列表
     *
     * @param kingBaseMeteringUnitEntity 金蝶-基础-计量单位
     * @return 金蝶-基础-计量单位
     */
    @Override
    public Page selectKingBaseMeteringUnitEntityList ( KingBaseMeteringUnitEntity kingBaseMeteringUnitEntity ) {
        Page page = this.page ( new Page<> ( kingBaseMeteringUnitEntity.getPage (), kingBaseMeteringUnitEntity.getLimit () ) );
        return page;
    }

    /**
     * 新增金蝶-基础-计量单位
     *
     * @param kingBaseMeteringUnitEntity 金蝶-基础-计量单位
     * @return 结果
     */
    @Override
    public int insertKingBaseMeteringUnitEntity ( KingBaseMeteringUnitEntity kingBaseMeteringUnitEntity ) {
        kingBaseMeteringUnitEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingBaseMeteringUnitEntity.setUserId ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseMeteringUnitEntity.setCreateTime ( DateUtils.getNowDate () );
        return kingBaseMeteringUnitMapper.insertKingBaseMeteringUnitEntity ( kingBaseMeteringUnitEntity );
    }

    /**
     * 修改金蝶-基础-计量单位
     *
     * @param kingBaseMeteringUnitEntity 金蝶-基础-计量单位
     * @return 结果
     */
    @Override
    public int updateKingBaseMeteringUnitEntity ( KingBaseMeteringUnitEntity kingBaseMeteringUnitEntity ) {
        return kingBaseMeteringUnitMapper.updateKingBaseMeteringUnitEntity ( kingBaseMeteringUnitEntity );
    }

    /**
     * 批量删除金蝶-基础-计量单位
     *
     * @param ids 需要删除的金蝶-基础-计量单位ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseMeteringUnitEntityByIds ( String[] ids ) {
        List<KingBaseMeteringUnitEntity> list = this.list ( new LambdaQueryWrapper<KingBaseMeteringUnitEntity> ().in ( KingBaseMeteringUnitEntity :: getId, ids ) );
        List<String> idList = list.stream ().filter ( unit -> unit.getIsDel () == 1 ).map ( KingBaseMeteringUnitEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( unit -> {
            if ( unit.getIsDel () == 2 ) {
                stringBuilder.append ( unit.getMeteringUnitName () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseMeteringUnitEntity> ().set ( KingBaseMeteringUnitEntity :: getChangeTime, DateUtils.getNowDate () ).in ( KingBaseMeteringUnitEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除计量单位失败" );
        }
        String str = "删除计量单位成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-基础-计量单位信息
     *
     * @param id 金蝶-基础-计量单位ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseMeteringUnitEntityById ( String id ) {
        return kingBaseMeteringUnitMapper.deleteKingBaseMeteringUnitEntityById ( id );
    }
}
